package com.fingard.dsp.bank.directbank.yqb01;

import com.fingard.dsp.bank.directbank.bankInterface.IContract;
import com.fingard.dsp.bank.directbank.vbao02.util.CryptTool;
import com.fingard.dsp.bank.directbank.yqb01.util.*;
import com.fingard.dsp.bank.reqrespobj.contract.*;
import com.fingard.dsp.bank.reqrespobj.contract.detail.ReqContractDetail;
import com.fingard.dsp.bank.reqrespobj.contract.detail.RetAtsContractDetail;
import com.fingard.util.HttpClientUtil;

import java.net.InetAddress;

/**
 * @author XueJy on 2018/9/3.
 * @version 1.0
 */
public class YQB01Contract extends YQB01Base implements IContract {

    @Override
    public RetAtsContract contract(ReqContract p_req) throws Exception {
        /** 步骤(01-签约申请, 02-签约确认) */
//        public String step;
        /** 法人姓名 */
//        public String legalName;
        /** 法人证件类型 */
//        public String legalCertType;
        /** 法人证件号码= */
//        public String legalCertNum;
        /** 合约到期日(格式:yyyyMMdd) */
//        public String deadLine;
        /** 签约后单笔代扣最大限额 */
//        public String singleLimit;
        /** 单笔代扣周期限制单位(1-日,2-月,3-年) */
//        public String periodLimit;
        /** 签约单笔代扣最大代扣次数(范围:1-9999) */
//        public String countLimit;
        /** 短信签约码 */
//        public String token;
        RetAtsContract retAts = new RetAtsContract(p_req.reqHeader);
        RetAtsContractDetail tmpRetDetail = new RetAtsContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;

        ReqContractDetail tmpReqDetail= p_req.contractDetail;
        YQB01ReqDto reqDto = new YQB01ReqDto();
        YQB01ReqContent reqContent = new YQB01ReqContent();

        //reqContent Params
        //发起渠道
        reqContent.setChannel(getBankConfig().getMapValueByKey("fieldRegister", "channel"));
        //来源系统名
        reqContent.setSystem(getBankConfig().getMapValueByKey("fieldRegister", "system"));
        //pc电脑ip
        InetAddress addr = InetAddress.getLocalHost();
        reqContent.setPcIp(addr.getHostAddress());
        //数据来源
        reqContent.setCoOperCode(getBankConfig().getMapValueByKey("fieldRegister", "coOperCode"));
        //对应商户号
        reqContent.setMerchantNo(getBankConfig().getMapValueByKey("fieldRegister", "merchantId"));
        //商户会员编号 pkufi
        reqContent.setPartnerId(getBankConfig().getMapValueByKey("fieldRegister", "partnerId"));
        //绑定手机号
        reqContent.setBindMobile(tmpReqDetail.cellPhone);
        //真实姓名 realName
        reqContent.setRealName(tmpReqDetail.payName);
        //证件类型identityType
        if ("0".equals(tmpReqDetail.certType)) {
            reqContent.setIdentityType("I");
            //证件号码identityNumber
            reqContent.setIdentityNumber(tmpReqDetail.certNum);
        } else {
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = "不支持的证件类型，该系统只支持身份证";
            retAts.writeRetDetail(tmpRetDetail);
            return retAts;
        }
        String content= JsonUtil.writeValue(reqContent);
        //reqdto Params
        reqDto.setHashFunc("SHA-1");
        //R0568域注册
        reqDto.setSystemId(getBankConfig().getMapValueByKey("fieldRegister","systemId"));
        reqDto.setServiceCode("R0568");
        String token= CryptTool.byteToHex(CryptTool.sha1Digest((content+getBankConfig().getMapValueByKey("fieldRegister","SHA-1Key")).getBytes("UTF-8")));
        reqDto.setToken(token);
        WriteBankLogLn("content明文" + content);
        String AESsaltKey = getBankConfig().getMapValueByKey("fieldRegister", "AESKey");
        content=  encryptByAES(AESsaltKey, content);
        reqDto.setContent(content);
        String reqMsg= JsonUtil.writeValue(reqDto);
        WriteBankLogStep2(reqMsg);
        String url = getBankConfig().getMapValueByKey("fieldRegister", "registerUrl");
        WriteBankUrlLogLn(url);
        String respMsgEnctypt= HttpClientUtil.executeJsonPost(url, reqMsg);
        if (!"".equals(respMsgEnctypt) && respMsgEnctypt != null) {
            WriteBankLogStep3(respMsgEnctypt);
            YQB01RespDto respDto= (YQB01RespDto) JsonUtil.readValue(respMsgEnctypt,YQB01RespDto.class);
            tmpRetDetail.respCode = "0";

            if ("000000".equals(respDto.getCode())) {
                String respMsg= decryptByAES(AESsaltKey, respDto.getContent()); WriteBankLogLn("返回content明文" + respMsg);
                YQB01RespContent respContent = (YQB01RespContent) JsonUtil.readValue(respMsg,YQB01RespContent.class);
                if ("000000".equals(respContent.getRespCode())) {
                    tmpRetDetail.signState = ContractState.SUCCESS;
                    tmpRetDetail.token = respContent.getCustomerId();
                    tmpRetDetail.respInfo = respContent.getMemo();
                    retAts.writeRetDetail(tmpRetDetail);
                    return retAts;
                } else {
                    tmpRetDetail.respInfo = respContent.getMemo();
                    tmpRetDetail.signState = ContractState.FAIL;
                    retAts.writeRetDetail(tmpRetDetail);
                    return retAts;
                }
            } else {
                tmpRetDetail.respInfo = respDto.getMemo();
                tmpRetDetail.signState = ContractState.FAIL;
                retAts.writeRetDetail(tmpRetDetail);
                return retAts;
            }
        } else {
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = "请求失败，返回结果为null";
            retAts.writeRetDetail(tmpRetDetail);
            return retAts;
        }
    }

    @Override
    public RetAtsContract upContract(ReqContract p_req) throws Exception {
        return null;
    }

    @Override
    public RetAtsContract qryContract(ReqContract p_reqQry) throws Exception {
        return null;
    }

    @Override
    public RetAtsQryBatContract qryBatContract(ReqQryBatContract p_reqQry) throws Exception {
        return null;
    }
}
